package form.utils
{
	/**	  
	 * Has functionality for string manipulation: truncate, strip white spaces
	 */
	 
	public class StringUtils
	{	
		public static function trim(string:String):String {
			if (string == null) { return null; }
			return string.replace(/(^\s*)|(\s*$)/g, "");
		}
		
		public static function ucfirst(string:String):String {
			if (string == null) { return null; }
			if (!string.length) { return string; }
			
			return string.charAt(0).toUpperCase() + string.substr(1).toLowerCase();
		}
		
		public static function lcfirst(string:String):String {
			if (string == null) { return null; }
			if (!string.length) { return string; }
			
			return string.charAt(0).toLowerCase() + string.substr(1).toUpperCase();
		}
		
		public static function isEmpty(string:String):Boolean {
			return (string == null || !string.length);
		}
		
		public static function isWhitespace(string:String):Boolean {
			return (StringUtils.isEmpty(string) || string.match(/^\s*$/));
		}
		
		public static function truncateString (text:String, maxLength:int):String {
			if(text.length > maxLength) {
					return StringUtils.stripWhiteSpacePrefixSuffix(text.substr(0,maxLength-3)) + "...";	//truncate if the length of the text is more than the maximum allowed				
			}
			else return text;
		}
		
		public static function truncate (text:String):String {			
			if(StringUtils.hasTruncate(text)) {	//before attempting to truncate, check that the text is set to be truncated
				var start:int = text.indexOf("<t maxlength=",0);	//token before the clean text
				var end:int = text.indexOf("</t>",0);	//token after the clean text
				
				var closeStartTruncateIndex:int = text.substring(start,end).indexOf(">") + 1 + start; //find index after the end of the opening <t...> tag
				var startTruncateTag:String = text.substring(start,closeStartTruncateIndex);
				var maxLength:int = getTruncateMaxLength(startTruncateTag);							
				
				var prefixText:String = stripWhiteSpacePrefixSuffix(text.substr(0,start));	//store HTML before the text
				var cleanText:String = stripWhiteSpacePrefixSuffix(text.substring(closeStartTruncateIndex,end));	//store the text cleaned from HTML tags via start/end tokens
				var suffixText:String = stripWhiteSpacePrefixSuffix(text.substring(end + 4, text.length));	//store HTML after the text
								
				if(cleanText.length > maxLength) {
					return prefixText + StringUtils.stripWhiteSpacePrefixSuffix(cleanText.substr(0,maxLength-3)) + "..." + suffixText;	//truncate if the length of the text is more than the maximum allowed				
				}
				else return prefixText + cleanText + suffixText;
			}
			else return text;
		}
		
		public static function getTruncateMaxLength (text:String):int {
			var start:int = text.indexOf("maxlength=") + 10;			
			var nextString:String = text.substring(start + 1, text.indexOf(">"));	//start search past 1st double quote
			var closeQuoteIndex:int = nextString.indexOf("\"");	//find next double quote			
					
			return int(nextString.substring(0,closeQuoteIndex));	//return substring between double quotes
		}
		
		public static function hasTruncate (text:String):Boolean {
			if((text.indexOf("<t maxlength=",0) > -1) && (text.indexOf("</t>",0) > -1) && (text.indexOf("<t maxlength=",0) < text.indexOf("</t>",0)))
				return true;
			else
				return false;
		}
		
		public static function stripWhiteSpacesBetweenTags (text:String):String {
			var tags:Array = text.split(">");
			text = "";
			
			for(var x:String in tags) {
				text += StringUtils.stripWhiteSpacePrefixSuffix(tags[x]);
				if(String(tags[x]).indexOf("<") != -1) text += ">";	//add closing tag that was cut away during split
			}
			
			return text;
		}
		
		protected static function stripWhiteSpacePrefixSuffix (text:String):String {			
			//strip prefix
			while(text.substr(0,1) == " ") {
				text = text.substring(1,text.length);
			}
			
			//strip suffix
			while(text.substring(text.length-1,text.length) == " ") {
				text = text.substring(0,text.length - 1);
			}
		
			return text;
		}
		
		public static function textWrapLyrics (text:String, lyricsLineLength:int): String {
			var lyrics:String = "";
			var lyricsArray:Array = text.split("<br/>");
			for(var line:String in lyricsArray) {	//loop through each line of lyrics to see if it needs to be split into 2 lines
				lyrics += StringUtils.splitLyricsLine(StringUtils.stripWhiteSpacePrefixSuffix(lyricsArray[line]),lyricsLineLength);
			}

			return "<lyrics xmlns:media=\"http://search.yahoo.com/mrss/\" xmlns:myspace=\"http://myspace.com/\">" + lyrics + "</lyrics>";
		}
		
		public static function textWrapByRowLength (text:String, rowLength:int, html:Boolean = false):String {
			var splitLine:String = "";
			var wordCutOffIndex:int;
			
			var token:String = html ? "<br />" : "\n";
			
			while(text.length > rowLength) {
				wordCutOffIndex = text.substr(0,rowLength).lastIndexOf(" ");	//find the index of the last space between words
				splitLine += text.substring(0,wordCutOffIndex) + token;
				text = text.substring(wordCutOffIndex + 1, text.length);
			}			
			// append last section of text.
			splitLine += text; 
			
			return splitLine;
		}
		
		public static function parseBoolean(value:String):Boolean {
			if (value == null)
				return false;
				
			if(isWhitespace(value))
				return false;
				
			switch(value.toLowerCase()) {
				case "": return false;
				case "true": return true;
				case "false": return false;
				case "t": return true;
				case "f": return false;
				case "1": return true;
				case "0": return false;
				default: return Boolean(value);
			}
		}
		
		protected static function splitLyricsLine (line:String, lyricsLineLength:int) : String {
			var splitLine:String = "";
			var wordCutOffIndex:int;
			
			if(line.indexOf("<lyrics") == -1 && line.indexOf("</lyrics>") == -1) {							
				while(line.length > lyricsLineLength) {					
					wordCutOffIndex = line.substr(0,lyricsLineLength).lastIndexOf(" ");	//find the index of the last space between words
					if(wordCutOffIndex == -1) // If there is no white space in the line break the line based on the next white space found					 
					{
						wordCutOffIndex = line.substr(lyricsLineLength, line.length).indexOf(" ");						
					}
					splitLine += line.substring(0,wordCutOffIndex) + "<br/>";
					line = line.substring(wordCutOffIndex + 1, line.length);
				}
				splitLine += line + "<br/>";	//append the remaining portion of the line
			}			
			return splitLine;
		}		
		
		public static function addDelimeter(num:Number, delimeter:String):String 
		{ 
			var numString:String = num.toString(); 
			var numArr:Array = numString.split("."); 
			var result:String = "";  
			var intLength:int = numArr[0].length; 
			
			while(intLength--)
			{ 
				if(intLength < numArr[0].length - 1 && (numArr[0].length - intLength - 1) % 3 == 0) 
					result = delimeter + result; 
				result = numArr[0].substr(intLength, 1) + result; 
			} 
			//Handles fractional portion of number
			if(numArr.length > 1) 
				result += "." + numArr[1]; 
			return(result); 
		}		

		public static function formatNumber(num : Number, delimeter : String):String
		{
			var formatedNumber:String = "";
			formatedNumber = addDelimeter(num , delimeter);
			
			return(formatedNumber);
		}
		
		public static function formatDate(feedDate:Date):String
		{
			var formatedDate:String = "";
			
			var day:Number = feedDate.getDate();
			var month:Number = feedDate.getMonth() + 1;
			var year:Number = feedDate.getFullYear();				
			formatedDate = month + "/" + day  + "/" + year;
			return(formatedDate);
		}	

	}
}